package com.jh3502.helper.function.home;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;

import com.jh3502.helper.R;
import com.jh3502.helper.base.BaseActivity;
import com.jh3502.helper.function.batch.mytask.MyTaskFragment;
import com.jh3502.helper.function.person.personlist.PersonListFragment;
import com.jh3502.helper.function.person.personnew.PersonNewActivity;
import com.jh3502.helper.utils.Utils;
import com.jh3502.helper.view.RoundImageView;
import com.jh3502.helper.view.VerticalViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import devlight.io.library.ntb.NavigationTabBar;

/**
 * author:harisucici on 2018/8/9 15:04
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class HomeMenuActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.home_menu_tabbar)
    NavigationTabBar homeMenuTabbar;
    @BindView(R.id.home_menu_viewpager)
    VerticalViewPager homeMenuViewpager;
    @BindView(R.id.home_menu_head)
    RoundImageView homeMenuHead;

    private List<Fragment> mFragmentList;
    private FragmentAdapter mFragmentAdapter;
    private Fragment fragment1, fragment2, fragment3, fragment4, fragment5;

    private List<Integer> iconList = new ArrayList<>();
    private String data;
    private String clueId;
    private List<Integer> showAddList = new ArrayList<>(2);
    private int showAdd = 0;

    private HashMap<String, Object> dataMap;
    private boolean assignFlag;

    @Override
    public void responseError(int code, String msg) {

    }

    @Override
    protected int setViewId() {
        return R.layout.activity_home_menu;
    }

    @Override
    protected void initView() {
        Utils.getInstance().startActivity(this, PersonNewActivity.class);
    }

    @Override
    protected void initData() {
        initUI();
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }


    private void initUI() {
        mFragmentList = new ArrayList<>(2);

        getFragmentMyTask();// 我的任务
        getFragmentDataCollection();// 数据采集
        getFragmentDataArrange();//数据整理
        getFragmentTaskManager();//任务管理
        getFragmentShapeManager();// 号型标准管理管理

        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        homeMenuViewpager.setOverScrollMode(View.OVER_SCROLL_NEVER);
        homeMenuViewpager.setAdapter(mFragmentAdapter);
        final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
        for (int i = 0; i < mFragmentList.size(); i++) {
            models.add(new NavigationTabBar.Model.Builder(
                    ContextCompat.getDrawable(this, iconList.get(i)),
                    Color.TRANSPARENT).build()
            );
        }

        homeMenuTabbar.setModels(models);
        homeMenuTabbar.setViewPager(homeMenuViewpager, 0);
        homeMenuViewpager.addOnPageChangeListener(this);

    }


    /**
     * 我的任务
     */
    private void getFragmentMyTask() {
        fragment1 = new MyTaskFragment();
        showAddList.add(0);
        Bundle bundle1 = new Bundle();
        bundle1.putString("data", data);
        fragment1.setArguments(bundle1);
        mFragmentList.add(fragment1);
        iconList.add(R.drawable.leftbar_mytask);
    }

    /**
     * 数据采集
     */
    private void getFragmentDataCollection() {
        fragment2 = new Fragment();
        showAddList.add(0);
        Bundle bundle2 = new Bundle();
        bundle2.putString("data", data);
        fragment2.setArguments(bundle2);
        mFragmentList.add(fragment2);
        iconList.add(R.drawable.leftbar_datacollection);
    }

    /**
     * 数据整理
     */
    private void getFragmentDataArrange() {
        fragment3 = new Fragment();
        showAddList.add(1);
        Bundle bundle3 = new Bundle();
        bundle3.putString("data", data);
        fragment3.setArguments(bundle3);
        mFragmentList.add(fragment3);
        iconList.add(R.drawable.leftbar_datasorting);
    }

    /**
     * 任务整理
     */
    private void getFragmentTaskManager() {
        fragment4 = new Fragment();
        showAddList.add(2);
        Bundle bundle4 = new Bundle();
        bundle4.putString("data", data);
        fragment4.setArguments(bundle4);
        mFragmentList.add(fragment4);
        iconList.add(R.drawable.leftbar_taskmanagement);
    }

    /**
     * 号型标准管理
     */
    private void getFragmentShapeManager() {
        fragment5 = new Fragment();
        showAddList.add(3);
        Bundle bundle5 = new Bundle();
        bundle5.putString("data", data);
        fragment5.setArguments(bundle5);
        mFragmentList.add(fragment5);
        iconList.add(R.drawable.leftbar_typestandard);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        showAdd = showAddList.get(position);
        Bundle bundle = new Bundle();
        if (position == 0 || position == showAddList.size() - 1) {//第一页或者最后一页
            bundle.putBoolean("setScrollViewScroll", true);
        } else {
            bundle.putBoolean("setScrollViewScroll", false);
        }
        mFragmentList.get(position).setArguments(bundle);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }


    @OnClick(R.id.home_menu_main)
    public void onHomeMenuMainClicked() {
        onBackPressed();
    }

    @OnClick(R.id.home_menu_setting)
    public void onHomeMenuSettingClicked() {
//        Utils.getInstance().startActivity(this,SettingActivity.class);
    }

    private class FragmentAdapter extends FragmentPagerAdapter {
        public FragmentAdapter(FragmentManager mFragmentManager) {
            super(mFragmentManager);
            notifyDataSetChanged();
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }
}
